package com.haidechizi.leetcode;

import sun.reflect.generics.tree.Tree;

import java.util.Stack;

/**
 * @author: wangdejun
 * @create: 2020-04-14 10:58
 **/
public class _1022_从根到叶的二进制数之和 {

    int sum = 0;

    public int sumRootToLeaf(TreeNode root) {
        sum(root, 0);
        return sum;
    }

    public void sum(TreeNode node, int num) {
        if (node == null) {
            return;
        }
        num += node.val;
        if (node.left == null && node.right == null) {
            sum += num;
        }
        sum(node.left, num * 2);
        sum(node.right, num * 2);
    }
}
